<?php

// Language codes defined here follow ISO-639-2 (3 digits)
// Codes retrieved from http://rs.gbif.org/vocabulary/iso/639-2.xml
	$language = array();

function build3DigitLanguageCode() {	

	$language_voc = "http://rs.gbif.org/vocabulary/iso/639-2.xml";
	
	$language_xml = new SimpleXMLElement($language_voc, NULL, TRUE);
	$language_xml->registerXPathNamespace('x', 'http://rs.gbif.org/thesaurus/');
	
	// Calculate the number of available concepts
	
	$language_number = count($language_xml->xpath('//x:concept'));
	
	$language_name = $language_xml->xpath('//x:preferred/x:term[1]/@dc:title');
	$language_identifier = $language_xml->xpath('//x:concept/@dc:identifier');
	$language_fullname = array();
	$language_code = array();
	global $language;
	
	foreach ($language_name as $simObj) {
		array_push($language_fullname, get_object_vars($simObj));
	}

	foreach ($language_identifier as $simObj) {
		array_push($language_code, get_object_vars($simObj));
	}

	for ( $i = 0 ; $i < $language_number ; $i++ ) {
		$key = $language_fullname[$i]["@attributes"]['title'];
		$value = $language_code[$i]["@attributes"]['identifier'];
		$language[$key] = $value;
	}
	
}

function get3DigitLanguageCode($name) {
	global $language;
	return $language[$name];
}
?>